
module ai {
	/**
	 * 判断是否输赢，或者返回
	 */
	export class WinUtils extends BaseClass {

		public constructor() {
			super();
		}
		/**
		 * 赢了返回赢了的位子
		 */
		public isWin(board) {
			let postion, type = 0;
			for (let i = 0; i < board.length && !type; i++) {
				for (let j = 0; j < board[i].length && !type; j++) {
					let t = board[i][j];
					postion = [i, j]
					if (t !== chess.ChessConst.role.empty) {
						type = this.isFive(board, [i, j], t);
						if (type) break;
					}
				}
			}

			if (!type) return false;
			if (type === 1) return [
				postion,
				[postion[0], postion[1] + 1],
				[postion[0], postion[1] + 2],
				[postion[0], postion[1] + 3],
				[postion[0], postion[1] + 4],
			]
			if (type === 2) return [
				postion,
				[postion[0] + 1, postion[1]],
				[postion[0] + 2, postion[1]],
				[postion[0] + 3, postion[1]],
				[postion[0] + 4, postion[1]],
			]
			if (type === 3) return [
				postion,
				[postion[0] + 1, postion[1] + 1],
				[postion[0] + 2, postion[1] + 2],
				[postion[0] + 3, postion[1] + 3],
				[postion[0] + 4, postion[1] + 4],
			]
			if (type === 4) return [
				postion,
				[postion[0] + 1, postion[1] - 1],
				[postion[0] + 2, postion[1] - 2],
				[postion[0] + 3, postion[1] - 3],
				[postion[0] + 4, postion[1] - 4],
			]

		}

		/** 
		 * 判断改位置的4个方向是否可以5子
		 */
		public isFive(board, position, role) {
			let len = board.length;
			let count = 1;

			let reset = function () {
				count = 1;
			}

			for (let i = position[1] + 1; true; i++) {
				if (i >= len) break;
				let t = board[position[0]][i];
				if (t !== role) break;
				count++;
			}


			for (let i = position[1] - 1; true; i--) {
				if (i < 0) break;
				let t = board[position[0]][i];
				if (t !== role) break;
				count++;
			}

			if (count >= 5) return 1;

			//纵向
			reset();

			for (let i = position[0] + 1; true; i++) {
				if (i >= len) {
					break;
				}
				let t = board[i][position[1]];
				if (t !== role) break;
				count++;
			}

			for (let i = position[0] - 1; true; i--) {
				if (i < 0) {
					break;
				}
				let t = board[i][position[1]];
				if (t !== role) break;
				count++;
			}


			if (count >= 5) return 2;
			// \\
			reset();

			for (let i = 1; true; i++) {
				let x = position[0] + i, y = position[1] + i;
				if (x >= len || y >= len) {
					break;
				}
				let t = board[x][y];
				if (t !== role) break;

				count++;
			}

			for (let i = 1; true; i++) {
				let x = position[0] - i, y = position[1] - i;
				if (x < 0 || y < 0) {
					break;
				}
				let t = board[x][y];
				if (t !== role) break;
				count++;
			}

			if (count >= 5) return 3;

			// \/
			reset();

			for (let i = 1; true; i++) {
				let x = position[0] + i, y = position[1] - i;
				if (x < 0 || y < 0 || x >= len || y >= len) {
					break;
				}
				let t = board[x][y];
				if (t !== role) break;
				count++;
			}

			for (let i = 1; true; i++) {
				let x = position[0] - i, y = position[1] + i;
				if (x < 0 || y < 0 || x >= len || y >= len) {
					break;
				}
				let t = board[x][y];
				if (t !== role) break;
				count++;
			}

			if (count >= 5) return 4;

			return 0;

		}

		/** 这种没看懂 */
		public getWinArray() {
			/*
 			* 赢法数组
 			*/
			let wins = [];
			for (let i = 0; i < 15; i++) {
				wins[i] = [];
				for (let j = 0; j < 15; j++) {
					wins[i][j] = [];
				}
			}
			let count = 0;
			/*
			 * 所有竖线的赢法
			 */
			for (let i = 0; i < 15; i++) {
				for (let j = 0; j < 11; j++) {
					for (let k = 0; k < 5; k++) {
						wins[i][j + k][count] = true;
					}
					count++;
				}
			}
			/*
			 * 所有横线的赢法
			 */
			for (let i = 0; i < 15; i++) {
				for (let j = 0; j < 11; j++) {
					for (let k = 0; k < 5; k++) {
						wins[j + k][i][count] = true;
					}
					count++;
				}
			}
			/*
			 * 所有斜线的赢法
			 */
			for (let i = 0; i < 11; i++) {
				for (let j = 0; j < 11; j++) {
					for (let k = 0; k < 5; k++) {
						wins[i + k][j + k][count] = true;
					}
					count++;
				}
			}
			/*
			 * 所有反斜线的赢法
			 */
			for (let i = 0; i < 11; i++) {
				for (let j = 14; j > 3; j--) {
					for (let k = 0; k < 5; k++) {
						wins[i + k][j - k][count] = true;
					}
					count++;
				}
			}
			return { wins, count };
		}
	}
}